// map.cpp
// 03 October 2011
// Purpose: Source for Map class

#include "map.h"

Map::Map()// Default Constructor,creates a blank map of 32 by 32
	:_h(32),_w(32),_map(new int[_h])
{
	for(int ii=0;ii<_h;ii++)//makes the map blank by filling it with zeros.
	{
		_map[ii]=0;
	}
}

Map::Map(const int & height,const int * map,const Coordinate & start,const Coordinate & finish)// Takes a height and 1D int array and constructs map.
	:_h(height),_map(new int[height]),_start(start),_finish(finish)
{
		for(int ii=0;ii<_h;ii++)//initilizes _map to the same values passed in
	{
		_map[ii]=map[ii];
	}
		findW();//initializes _w
}

Map::Map(const Map & copy)//copy constructor for map of the same class.
	:_h(copy.getH()),_map(new int[_h]),_w(getW())
{
	for(int ii=0;ii<_h;ii++)//initilizes _map to the same values passed in from our copy
	{
		_map[ii]=copy._map[ii];
	}
	findW();//initializes _w
}
/*
Map::Map(const MapGen & copy)//copy constructor that takes a mapGen object.
	:_h(copy.getH()),_w(copy.getW()),_map(new int[_h])
{
	for(int jj=0;jj<_h;jj++)
	{
	for(int ii=0;ii<_w;ii++)//initilizes _map to the same values passed in
	{
		//Set bits here_map[ii]=copy.getValue(ii,jj);
	}
	}
}
*/
const Coordinate & Map::getStart() const//returns starting position of map
{
return _start;
}

const Coordinate & Map::getFinish() const//returns finish position of map
{
return _finish;
}

void Map::displayMap(ostream & os) const		// Prints map to cout
{
	for(int ii=0;ii<_h;ii++)
	{
		for(int jj=32;jj>0;jj--)
		{
			if(_map[ii]&(1<<(jj-1)))
			{
				os<<"1";
			}
			else
			{
				os<<"0";
			}
		}
	os<<std::endl;
	}

}

int Map::getH() const//returns height of map
{
	return _h;
}

int Map::getW() const
{
	return _w;
}

void Map::mapImport(const int & height,const int * map)//takes a size and 1-d array
{
	delete [] _map;//destorys current map

	_h=height; //assigns new height value
	_map=new int[height]; //allocates memory for the new map
	for(int ii=0;ii<_h;ii++)//initilizes _map to the same values passed in
	{
		_map[ii]=map[ii];
	}
	findW();//initializes _w
}

int * Map::getMap() const//returns a copy of _map
{
	return _map;
}

void Map::findW()
{
		for(int ii=32;ii>0;ii--)
	{
		if(!(_map[0]&(1<<(ii))))
		{
			_w=ii;
		}

	}
}